package com.xtaller.easy.admin.controller.sys;

import com.alibaba.fastjson.JSONObject;
import com.xtaller.common.convert.J;
import com.xtaller.common.convert.V;
import com.xtaller.common.convert.W;
import com.xtaller.easy.admin.base.TApi;
import com.xtaller.easy.admin.config.R;
import com.xtaller.easy.admin.model.SysConfigLabel;
import com.xtaller.easy.admin.service.impl.SysConfigLabelService;
import com.xtaller.easy.admin.service.impl.SysLabelService;
import com.xtaller.easy.admin.specification.ConfigLabelOpt;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by Taller on 2018/5/14
 */
@Api(tags = "系统基础配置相关api")
@RestController
@RequestMapping("/v1/sys")
@CrossOrigin   //跨域服务注解
public class ConfigLabelApi extends TApi {
    @Autowired
    private SysLabelService labelService;
    @Autowired
    private SysConfigLabelService configLabelService;

    @GetMapping("/config")
    @ApiOperation(value = "获取配置信息")
    public Object getConfigLabel(){
        JSONObject object = labelService.getLabelByCode("system");
        List<JSONObject> labels = J.o2l(object.get("children"));
        return R.ok("读取成功", formatLabel(labels));
    }

    private List<JSONObject> formatLabel(List<JSONObject> labels){
        // 获取所有当前标签的数据
        List<SysConfigLabel> configLabels = configLabelService.query(
                W.f(
                        W.and("1", "eq", "1")
                )
        );
        // 处理基础标签数据
        if(labels.size() > 0){
            // 一个个分类处理
            for(JSONObject label:labels){
                List<JSONObject> childrens = J.o2l(label.get("children"));
                if(childrens.size() > 0){
                    for(JSONObject children:childrens){
                        // 数据比较入萝卜坑
                        children.put("val", "");
                        children.put("txt", "");
                        if(configLabels.size() > 0){
                            for(SysConfigLabel configLabel:configLabels){
                                if(V.isEqual(children.getString("id"), configLabel.getLabelId())){
                                    children.put("val", configLabel.getVal());
                                    children.put("txt", configLabel.getVal());
                                    if(children.getInteger("type") == 9){
                                        List<JSONObject> options = J.o2l(children.get("options"));
                                        if(options.size() > 0){
                                            for(JSONObject option:options){
                                                if(option.getString("val").equals(configLabel.getVal())){
                                                    children.put("txt", option.getString("txt"));
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                label.put("children", childrens);
            }
        }
        return labels;
    }

    @PostMapping("/config")
    @ApiOperation(value = "配置信息")
    public Object saveConfigLabel(@RequestBody ConfigLabelOpt object){
        List<SysConfigLabel> configLabels = configLabelService.query(
                W.f(
                        W.and("labelId", "eq", object.getLabelId())
                )
        );
        SysConfigLabel label = new SysConfigLabel();
        if(configLabels.size() == 0){
            label = J.o2m(object, SysConfigLabel.class);
            label.setCreator(getUserIdByToken());
            configLabelService.createSysConfigLabel(label);
        }else{
            label = configLabels.get(0);
            label.setVal(object.getVal());
            label.setReviser(getUserIdByToken());
            configLabelService.updateSysConfigLabel(label);
        }
        if(label == null)
            return R.error("配置信息失败,请稍后再试");
        return R.ok("配置信息成功");
    }

}
